package com.example.administrator.szb.util;

import android.net.Uri;
import android.os.Environment;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;

/**
 * Created by stone on 2018/1/24.
 */

public class PhotoUtils {
    //https://github.com/crazycodeboy/TakePhoto#%E5%AE%89%E8%A3%85%E8%AF%B4%E6%98%8E
    //compile 'com.jph.takephoto:takephoto_library:4.0.3'
    int type = 0;//0拍照  1相册
    private int limit = 1;
    private boolean crop = true;

    public void setType(int type) {
        this.type = type;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public void setCrop(boolean crop) {
        this.crop = crop;
    }

    public void onClick(int type, TakePhoto takePhoto) {
        File file=new File(Environment.getExternalStorageDirectory(), "/szbfile/com.example.administrator.szb/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        configCompress(takePhoto);
        configTakePhotoOption(takePhoto);
        switch (type){
            case 1:
                if(limit>1){
                    if(crop){
                        takePhoto.onPickMultipleWithCrop(limit,getCropOptions(crop));
                    }else {
                        takePhoto.onPickMultiple(limit);
                    }
                    return;
                }
                if(crop){
                    takePhoto.onPickFromGalleryWithCrop(imageUri,getCropOptions(crop));
                }else {
                    takePhoto.onPickFromGallery();
                }
                break;
            case 0:
                if(crop){
                    takePhoto.onPickFromCaptureWithCrop(imageUri,getCropOptions(crop));
                }else {
                    takePhoto.onPickFromCapture(imageUri);
                }
                break;
            default:
                break;
        }
    }
    private void configCompress(TakePhoto takePhoto){
        //不压缩takePhoto.onEnableCompress(null,false);
//1048576
        /*if (!crop){
            takePhoto.onEnableCompress(null,false);
            return;
        }*/
        int maxSize= 1048576;
        int width= 800;
        int height= 800;
        //显示压缩进度条
        boolean showProgressBar=false;
        //拍照压缩后是否保存原图
        boolean enableRawFile = true;
        CompressConfig config=new CompressConfig.Builder()
                .setMaxSize(maxSize)
                .setMaxPixel(width>=height? width:height)
                .enableReserveRaw(enableRawFile)
                .create();
        takePhoto.onEnableCompress(config,showProgressBar);


    }

    private void configTakePhotoOption(TakePhoto takePhoto){
        TakePhotoOptions.Builder builder=new TakePhotoOptions.Builder();
        //使用TakePhoto自带相册
        builder.setWithOwnGallery(true);
        //纠正拍照的照片旋转角度
        builder.setCorrectImage(true);
        takePhoto.setTakePhotoOptions(builder.create());

    }
    private CropOptions getCropOptions(boolean flag){
        if(!flag)return null;

        //裁剪
        int height= 800;
        int width= 800;
        //TakePhoto自带
        boolean withWonCrop=true;

        CropOptions.Builder builder=new CropOptions.Builder();
        builder.setAspectX(width).setAspectY(height);

         builder.setOutputX(width).setOutputY(height);//比例

        builder.setWithOwnCrop(withWonCrop);
        return builder.create();
    }


}
